package MediaOnline.VideoPlayer;

import java.io.File;
import java.util.ArrayList;

import MediaOnline.GUI.Nahi_MediaActivity;
import MediaOnline.GUI.R;
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.MediaStore;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.Toast;

public class VideoListViewActivity extends Activity implements
		OnItemClickListener {
	/** Called when the activity is first created. */

	Cursor cursor;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.listview);
		// tao mot list de hien danh sach video
		ListView listView = (ListView) this.findViewById(R.id.ListView);
		// tao mot danh sach cot ma chung ta muon hien
		String[] thumbColumn = { MediaStore.Video.Thumbnails.DATA,
				MediaStore.Video.Thumbnails.VIDEO_ID

		};
		// tao mang hien ve thong so cua video
		String[] mediaColumns = { MediaStore.Video.Media._ID,
				MediaStore.Video.Media.DATA, MediaStore.Video.Media.TITLE,
				MediaStore.Video.Media.MIME_TYPE };
		// load tat ca nhung video o thiet bi
		cursor = managedQuery(MediaStore.Video.Media.EXTERNAL_CONTENT_URI,
				mediaColumns, null, null, null);
		// O moi dong se tuong ung voi thong so trong videoViewInfo
		ArrayList<VideoViewInfo> videoRows = new ArrayList<VideoViewInfo>();
		//
		if (cursor.moveToFirst()) {
			do { // dung do while de chay tu dong dau toi dong ke tiep
				VideoViewInfo newVVI = new VideoViewInfo(); // tao mot doi tuong
															// newVVI va moi
															// thong so nay se
															// duoc chua o
															// videoViewinfo
				int id = cursor.getInt(cursor
						.getColumnIndex(MediaStore.Video.Media._ID));
				Cursor thumbCursor = managedQuery(
						MediaStore.Video.Thumbnails.EXTERNAL_CONTENT_URI,
						thumbColumn, MediaStore.Video.Thumbnails.VIDEO_ID + "="
								+ id, null, null);
				if (thumbCursor.moveToFirst()) {
					newVVI.thumbPath = thumbCursor.getString(thumbCursor
							.getColumnIndex(MediaStore.Video.Thumbnails.DATA));
					Log.v("Tag", "Title:" + newVVI.title);
				}
				newVVI.filePath = cursor
						.getString(cursor
								.getColumnIndexOrThrow(MediaStore.Video.Thumbnails.DATA));
				newVVI.title = cursor.getString(cursor
						.getColumnIndexOrThrow(MediaStore.Video.Media.TITLE));

				newVVI.mimeType = cursor
						.getString(cursor
								.getColumnIndexOrThrow(MediaStore.Video.Media.MIME_TYPE));
				Log.v("Video gallery", "MINE" + newVVI.mimeType);
				// cuoi cung la them vao mang videoRows kieu arrayList nhu khai
				// bao tren
				videoRows.add(newVVI);

			} while (cursor.moveToNext());

		}
		listView.setAdapter(new VideoGalleryAdapter(this, videoRows));
		listView.setOnItemClickListener(this);
	}
	//
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode != KeyEvent.KEYCODE_VOLUME_UP
				&& keyCode != KeyEvent.KEYCODE_VOLUME_DOWN
				&& keyCode != KeyEvent.KEYCODE_MENU
				&& keyCode != KeyEvent.KEYCODE_CALL
				&& keyCode != KeyEvent.KEYCODE_ENDCALL ) {
			if (keyCode == KeyEvent.KEYCODE_HEADSETHOOK) {
				
			} else if (keyCode == KeyEvent.KEYCODE_BACK) {
				finish();
			}

			else {
				// toggleMediaControlsVisiblity();
			}
		}

		return super.onKeyDown(keyCode, event);
	}
	/**
	 * Khi item duoc click thi onItemClick se duoc goi. O day se dung Intent
	 * danh kem duong dan, kieu dinh dang va goi chuong trinh video mac dinh o
	 * thiet bi de chay
	 */
	public void onItemClick(AdapterView<?> l, View v, int position, long id) {
		// TODO Auto-generated method stub
		if (cursor.moveToPosition(position)) {
			int fileColumn = cursor
					.getColumnIndexOrThrow(MediaStore.Video.Media.DATA);
			int mineColumn = cursor
					.getColumnIndexOrThrow(MediaStore.Video.Media.MIME_TYPE);
			String videoFilePath = cursor.getString(fileColumn);
			Log.d("Path:::::::::::::", videoFilePath);
			Toast.makeText(this, videoFilePath, Toast.LENGTH_LONG);
			String mineType = cursor.getString(mineColumn);
			Intent intent = new Intent(VideoListViewActivity.this,
					Nahi_MediaActivity.class);
			File newFile = new File(videoFilePath);
			Bundle newBundle = new Bundle();
			newBundle.putString("Link", newFile.toString());
			intent.putExtras(newBundle);
			startActivity(intent);

		}
	}
}